package com.duonangshen.tx2019;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/*
 *Create 2019/02/23 21:00
 *order by bamboo_lan
 */

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    final OkHttpClient client = new OkHttpClient();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.query_name).setOnClickListener(this);
        //findViewById(R.id.bt_post).setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            //case R.id.bt_get:
            //    getRequest();
            //    break;

            case R.id.query_name:
                postRequest();
                break;
        }
    }
    //预留通过号码查询姓名的功能类
    private void getRequest() {

        final Request request=new Request.Builder()
                .get()
                .tag(this)
                .url("http://www.duonangshen.com")
                .build();

        new Thread(new Runnable() {
            @Override
            public void run() {
                Response response = null;
                try {
                    response = client.newCall(request).execute();
                    if (response.isSuccessful()) {
                        Log.i("WY","打印GET响应的数据：" + response.body().string());
                    } else {
                        throw new IOException("Unexpected code " + response);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }

    //post类
    private void postRequest() {
        //获取输入
        EditText etname = findViewById(R.id.editTextName);
        String str_name =etname.getText().toString();
        //构造地址
        RequestBody requestBody = new FormBody.Builder()
                .add("name", str_name)
                .build();
        Request request = new Request.Builder()
                .url("http://192.168.43.13/contact/get.php")
                .post(requestBody)
                .build();

        /* 下边和get一样了 */
        client.newCall(request).enqueue(new Callback() {
            public void onResponse(Call call, Response response) throws IOException {
                final String bodyStr = response.body().string();
                final boolean ok = response.isSuccessful();
                runOnUiThread(new Runnable() {
                    public void run() {
                        if (ok) {
                            //Toast.makeText(MainActivity.this, bodyStr, Toast.LENGTH_SHORT).show();
                            //解析json，并准确提取需要值，显示在textview控件上
                            JSONObject object= JSON.parseObject(bodyStr);
                            String phone=object.getString("oa_phone");
                            Toast.makeText(MainActivity.this, phone, Toast.LENGTH_SHORT).show();
                            //写入textview
                            TextView phView = findViewById(R.id.oaphone);
                            phView.setText(phone);
                        } else {
                            Toast.makeText(MainActivity.this, "server error : " + bodyStr, Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }

            public void onFailure(Call call, final IOException e) {
                runOnUiThread(new Runnable() {
                    public void run() {
                        Toast.makeText(MainActivity.this, "error : " + e.toString(), Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }

}
